//
//  BannerHeader.swift
//  Eternity
//
//  Created by cll12_12 on 2016/12/1.
//  Copyright © 2016年 ZhengWei. All rights reserved.
//

import UIKit
import SDCycleScrollView

class BannerHeader: UICollectionReusableView {
    
    var linkArr: [String]?
    var titleArr: [String]?
    
    
    // MARK: - life cycle
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initBaseLayout()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - private method
    func initBaseLayout() {
        
        //imageNameArr = ["http://dynamic-image.yesky.com/1080x-/uploadImages/2016/023/17/9U5NOZ6SR4F7.jpg", "http://hiphotos.baidu.com/qxshch/pic/item/b236bfc8b8c6d90f7e3e6f30.jpg", "http://img.taopic.com/uploads/allimg/120427/53163-12042FZI917.jpg", "http://pic66.nipic.com/file/20150511/20797049_132511754000_2.jpg", "http://n1.itc.cn/img8/wb/recom/2016/01/02/145170542406608280.JPEG"]
        
        self.addSubview(bannerScroll)
    }
    
    func layoutPageSubviews() {
    
    }
    
    func configUI(imageUrlArr: [String]) {
        _bannerScroll.imageURLStringsGroup = imageUrlArr
    }
    
    // 获取view所属的viewController
    func controller(view:UIView)->UIViewController?{
        var next: UIView? = view
        repeat {
            if let nextResponder = next?.next, nextResponder.isKind(of: UIViewController.self) {
                return (nextResponder as! UIViewController)
            }
            next = next?.superview
            
        } while next != nil
        return nil
    }
    
    // MARK: - setters and getters
    var _bannerScroll: SDCycleScrollView!
    var bannerScroll: SDCycleScrollView {
        if _bannerScroll == nil {
            _bannerScroll = SDCycleScrollView(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 175), delegate: self, placeholderImage: UIImage(named: "def_375x170"))
            _bannerScroll.showPageControl = true
            _bannerScroll.pageControlDotSize = CGSize(width: 7, height: 7)
            _bannerScroll.pageDotImage = UIImage(named: "icon_dot_n")
            _bannerScroll.currentPageDotImage = UIImage(named: "icon_dot_h")
            
        }
        return _bannerScroll
    }
    
}
extension BannerHeader: SDCycleScrollViewDelegate {
    
    func cycleScrollView(_ cycleScrollView: SDCycleScrollView!, didSelectItemAt index: Int) {
        let detailVC = BannerDetailViewController()
        detailVC.link = linkArr?[index]
        detailVC.title = titleArr?[index]
        detailVC.hidesBottomBarWhenPushed = true
        let viewController = controller(view: self)
        viewController?.navigationController?.pushViewController(detailVC, animated: true)
    }
}


